package com.gafker.threads;

import java.util.Random;

public class PrintTask implements Runnable {

	private final int sleepTime; // random sleep time for thread

	private final String taskName; // name of task

	private final static Random generator = new Random();

	public PrintTask(String name)

	{

		taskName = name; // set task name

		// pick random sleep time between 0 and 5 seconds

		sleepTime = generator.nextInt(5000); // milliseconds

	} // end PrintTask constructor

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		try {
			System.out.printf("%s going to sleep for %d milliseconds.\n", taskName, sleepTime);
			Thread.sleep(sleepTime);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			System.out.printf("%s %s\n", taskName, "terminated prematurely due to interruption");
			e.printStackTrace();
		}
		System.out.printf("%s done sleeping\n", taskName);
	}

	// method run contains the code that a thread will execute
}// end class PrintTask
